package com.hliushi.crawler;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @ClassName JsonpTest
 * @Description 演示使用Jsoup实现页面解析
 * @Author hliushi
 * @Date 2021/6/30 20:50
 * @Version V1.0
 **/
public class JsonpTest {

    @Test
    public void testGetDocument() throws IOException {
        // 1.
        // Document document = Jsoup.connect("http://www.baidu.com").get();
        Document document = Jsoup.parse(new URL("http://www.itcast.cn/"), 1000);
        Document document2 = Jsoup.parse(new File("src/main/resources/jsoup.html"), "UTF-8");

        String htmlStr = FileUtils.readFileToString(new File("src/main/resources/jsoup.html"), "UTF-8");
        System.out.println("htmlStr = " + htmlStr);

        Element titleElement = document.getElementsByTag("title").first();
        Element titleElement2 = document2.getElementsByTag("title").first();

        String title = titleElement.text();
        String title2 = titleElement2.text();

        System.out.println("title = " + title);
        System.out.println("title2 = " + title2);
    }


    @Test
    public void testGetElement() throws IOException {
        Document doc = Jsoup.parse(new File("src/main/resources/jsoup.html"), "UTF-8");
        // System.out.println(doc);

        // 根据id获取元素 getElementById
        Element element = doc.getElementById("city_bj");
        String text = element.text();
        System.out.println("text = " + text);

        // 根据标签获取元素 getElementsByTag
        Elements elements = doc.getElementsByTag("title");
        Element title = elements.first();

        // title = <title>传智播客官网-一样的教育,不一样的品质</title>
        System.out.println("title = " + title);

        // title.text() = 传智播客官网-一样的教育,不一样的品质
        System.out.println("title.text() = " + title.text());

        // 根据class获取元素 getElementsByClass
        Element last = doc.getElementsByClass("s_name").last();
        // last = 广州
        System.out.println("last = " + last.text());

        // 根据属性获取元素 getElementsByAttribute
        Element attrABC = doc.getElementsByAttribute("abc").first();
        System.out.println("attrABC = " + attrABC.text());
    }

    @Test
    public void testElementOperator() throws Exception {
        Document doc = Jsoup.parse(new File("src/main/resources/jsoup.html"), "UTF-8");
        Element element = doc.getElementsByAttributeValue("class", "city_con").first();

        // 获取元素中的id
        String id = element.id();
        System.out.println("id = " + id);

        // 获取元素中的className
        String className = element.className();
        System.out.println("className = " + className);

        // 获取元素中的属性值
        String attrIdValue = element.attr("id");
        System.out.println("attrIdValue = " + attrIdValue);   // attrIdValue = city_id

        // 从元素中获取所有的属性
        String attrs = element.attributes().toString();
        // attrs =  id="city_id" class="city_con" style="display: none;"
        System.out.println("attrs = " + attrs);

        // 获取元素中文本内容
        String text = element.text();
        System.out.println("text = " + text);
    }


    @Test
    public void testSelect() throws IOException {
        Document doc = Jsoup.parse(new File("src/main/resources/jsoup.html"), "UTF-8");

        // 根据标签名获取元素
        Elements spans = doc.select("span");

        for (Element span : spans) {
            System.out.println("span = " + span.text());
        }

        // 根据id获取元素
        String byId = doc.select("#city_bj").text();
        System.out.println("byId = " + byId);

        // 根据class获取元素
        String byClass = doc.select(".s_name").text();

        /**
         * Elements.text()  获取所有匹配元素的组合文本
         * byClass = 北京 上海 广州
         */
        System.out.println("byClass = " + byClass);

        // 根据属性获取元素
        String byAttr = doc.select("[abc]").text();
        System.out.println("byAttr = " + byAttr);

        // 根据属性值获取元素
        String byAttrValue = doc.select("[class=s_name]").first().text();
        System.out.println("byAttrValue = " + byAttrValue);
    }

    @Test
    public void testSelectHigh() throws IOException {
        Document doc = Jsoup.parse(new File("src/main/resources/jsoup.html"), "UTF-8");
        // 根据元素标签名 + id组合选取元素
        String tagWithId = doc.select("li#test").text();
        System.out.println("tagWithId = " + tagWithId);

        // 根据元素标签名 + class
        String tagWithClass = doc.select("div.city_con").text();
        System.out.println("tagWithClass = " + tagWithClass);

        // 根据元素标签名 + 元素名
        String text2 = doc.select("span[abc]").text();
        System.out.println("text2 = " + text2);

        // 任意组合
        String random = doc.select("span[abc].s_name").text();
        System.out.println("random = " + random);

        // 查找某个元素下的直接子元素    >
        String text = doc.select(".city_con > ul > li").text();
        System.out.println("text = " + text);   // text = 北京 上海 广州

        // 查找某个元素下的所有子元素
        String text3 = doc.select(".city_con li").text();
        System.out.println("text3 = " + text3); // text3 = 北京 上海 广州 天津

        String text1 = doc.select(".city_con > *").text();
        System.out.println("text1 = " + text1);
    }
}
